<template>
	<div class="login">
		<el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
			<h3 class="title">{{ title }}</h3>
			<el-form-item prop="username">
				<el-input  :prefix-icon="User" v-model="loginForm.username" type="text" placeholder="请输入账号">
				</el-input>
			</el-form-item>
			<el-form-item prop="password">
				<el-input  v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码"
					@keyup.enter.native="handleLogin(loginFormRef)">
					<template #prefix>
						<el-icon class="el-input__icon"><lock /></el-icon>
					</template>
				</el-input>
			</el-form-item>
			<el-form-item prop="code">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin(loginFormRef)"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>
			<el-form-item style="width: 100%">
				<el-button :loading="loading" size="medium" type="primary" style="width: 100%"
					@click.native.prevent="handleLogin(loginFormRef)">
					<span v-if="!loading">登 录</span>
					<span v-else>登 录 中...</span>
				</el-button>
			</el-form-item>

		</el-form>
	</div>
</template>

<script setup lang="ts" name="login">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from 'element-plus'
import { User } from '@element-plus/icons-vue'
import { Login } from "@/api/interface";
import { login,getCodeImg} from "@/api/login"
import { UserStore } from "@/store/modules/user"
import { ElNotification } from 'element-plus'
import { useRoute } from 'vue-router'
import router from '@/routers'
const title = ref('欢迎你');
const loginForm = reactive<Login.ReqLoginForm>({ username: "", password: "",code: '',key: '' });
const loginRules = reactive({
	username: [{ required: true, message: "请输入账号", trigger: "blur" }],
	password: [{ required: true, message: "请输入密码", trigger: "blur" }]
});
const loginFormRef = ref<FormInstance>()
const codeUrl= ref('');
const loading = ref(false);
const userStore = UserStore()
const UseRoute = useRoute()
const getCode =async () => {
	let res =await getCodeImg()
	codeUrl.value = res.data.code
	loginForm.key = res.data.key;
}
const handleLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
			try {
				loading.value = true;
				let res =await userStore.Login(loginForm)
				console.log(res,'res')
				router.push({ path: "/" }).catch(() => {});
				ElNotification({
					title: '成功',
					message: '登录成功',
					type: 'success',
				})
			} catch (error) {
				getCode()
			} finally {
				loading.value = false;
			}
    } else {
      console.log('error submit!', fields)
    }
  })
}
onMounted(async () => {
  getCode()
});
</script>

<style scoped lang="scss">
.login {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100%;
	background: url("../../assets/images/loginBg.jpg") center center no-repeat;
	// background-size: auto 100%;
	background-size: cover;
}
.login-form {
	width: 300px;
	.title {
		color: aliceblue;
		text-align: center;
	}
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
	margin-left: 12px;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
</style>